﻿using System;
using System.Collections;
using OME.Storage;
using System.Collections.Generic;

namespace OME
{
    public class BizDomain
    {
        //Hashtable to store order processor instances
        private Hashtable oprocItems = Hashtable.Synchronized(new Hashtable());
        //array of order processor name to be created under this biz domain
        public string[] oprocNames;
        //creation of order book
        private OrderBook orderBook = new OrderBook();
        private Container buyBook;
        private Container sellBook;
        public EquityMatchingEngine.EquityMatchingLogic logic;
        public List<Order> orderList = new List<Order>();
        public QuoteServer qs;
        public int processing;

        public BizDomain(string domainName, string[] workNames)
        {
           processing = 0;
           oprocNames = workNames;
           qs = new QuoteServer(10, this);
            
        }

        public OrderBook OrderBook
        {
            get { return orderBook; }
        }

        public Container BuyBook
        {
           get { return buyBook; }
           set { buyBook = value; }
        }

        public Container SellBook
        {
           get { return sellBook; }
           set { sellBook = value; }
        }

        public void Start()
        {
           OrderProcessor wrkObj;
            //Iterate thru all order processor names and 
            //create a new order processor object
            for (int ctr = 0; ctr < oprocNames.Length; ctr++)
            {
                //Instantiates new order processor that in turn creates a 
                //dedicated thread and queue
                wrkObj = new OrderProcessor(this, oprocNames[ctr]);
                oprocItems[oprocNames[ctr]] = wrkObj;
            }
            
            qs.StartListener();
            

            //if (service.Length > 5)
               //wrkObj.qs.BuildOrder(service);

            
        }

        //A fa鏰de method to the outside world, 
        //through which orders are submitted and queued up in 
        //appropriate order processor. 
        public void SubmitOrder(string procName, Order order)
        {
            OrderProcessor orderProcessor = oprocItems[procName] as OrderProcessor;
            orderProcessor.EnQueue(order);
        }

    }
}
